<?php

/**
 * match actions.
 *
 * @package    eveladder
 * @subpackage match
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class challengeActions extends sfActions
{
  /* displaying list */
  public function executeList()  {
  	$c = new Criteria() ;
  	$c->add(ChallengePeer::IS_VALID, 1) ;
  	$c->addDescendingOrderByColumn(ChallengePeer::CREATED_AT) ;

		$pager = new sfPropelPager('Challenge', sfConfig::get('app_pager_challenges_list'));
		$pager->setCriteria($c);
		$pager->setPage($this->getRequestParameter('page', 1));
		$pager->setPeerMethod('doSelectWithPlayers') ;
		$pager->init();
		$this->pager = $pager ;

  	$this->challenges = $pager->getResults() ;
  }
  
  /* displaying the rules */
  public function executeRules()	{
  }
  
  /* displaying a challenge details */
  public function executeDetails()	{
  	if ($this->getRequest()->getParameter('challenge'))	{
			/* loading the challenge */
	  	$c = new Criteria() ;
	  	$c->add(ChallengePeer::ID, $this->getRequest()->getParameter('challenge')) ;
	  	$this->challenge = ChallengePeer::doSelectOneWithPlayers($c) ;
  	}
  }

	/* displaying the ranking */
  public function executeRanking()	{
  	$c = new Criteria() ;
  	$c->addDescendingOrderByColumn(PlayerPeer::POINTS) ;
  	$this->ranking = PlayerPeer::doSelectJoinsfGuardUser($c) ;
  }

	/* new challenge */
  public function handleErrorPost()	{
  	$this->initPlayersList() ;
  	return sfView::SUCCESS ;
  }

  public function executePostConfirmation()	{
  }

  public function executePost()	{
  	if ($this->getRequest()->getMethod() == sfRequest::POST)	{
  		$c = new Criteria() ;
  		$c->add(PlayerPeer::USER_ID, $this->getUser()->getGuardUser()->getId()) ;
  		$p1 = PlayerPeer::doSelectOne($c) ;
			
  		$c = new Criteria() ;
  		$c->add(PlayerPeer::USER_ID, $this->getRequest()->getParameter('p2')) ;
  		$p2 = PlayerPeer::doSelectOne($c) ;
  		
  		// creating the challenge
  		$challenge = new Challenge() ;
  		$challenge->setP1Id($p1->getUserId()) ;
  		$challenge->setP2Id($p2->getUserId()) ;
  		$challenge->setP1Rank($p1->getRank()) ;
  		$challenge->setP2Rank($p2->getRank()) ;
  		$challenge->setP1Points($p1->getPoints()) ;
  		$challenge->setP2Points($p2->getPoints()) ;

  		// creating the killmail
  		$killmail = new Killmail() ;
  		$killmail->setContent($this->getRequest()->getParameter('killmail')) ;
  		$killmail->save() ;

  		$challenge->setKillmailId($killmail->getId()) ;
  		
  		$challenge->setIsValid(0) ;
  		$challenge->setIsRefused(0) ;
  		$challenge->save() ;
  		
  		$this->redirect('challenge/postConfirmation') ;
  	}
  	
  	$this->initPlayersList() ;
  }
  
  public function initPlayersList() {
  	/* getting the players list */
  	$this->players = mysfGuardUserPeer::doSelectOptions(new Criteria()) ;
  	unset($this->players[$this->getUser()->getGuardUser()->getId()]) ;
  }

	/* validation */
	public function handleErrorValidate()	{
		/* loading the challenge */
  	$c = new Criteria() ;
  	$c->add(ChallengePeer::ID, $this->getRequest()->getParameter('challenge')) ;
  	$this->challenge = ChallengePeer::doSelectOneWithPlayers($c) ;

		return sfView::SUCCESS ;
	}

	public function executeValidate()	{
		/* loading the challenge */
  	$c = new Criteria() ;
  	$c->add(ChallengePeer::ID, $this->getRequest()->getParameter('challenge')) ;
  	$this->challenge = ChallengePeer::doSelectOneWithPlayers($c) ;
  	
  	if (!$this->challenge)	{
  		$this->forward404() ;
  	}
  	
  	if ($this->getRequest()->getMethod() == sfRequest::POST)	{
	  	if ($this->getUser()->getGuardUser()->getId() != $this->challenge->getP2Id())	{
	  		$this->getRequest()->setError('authorization', 'you are not authorized to validate this challenge') ;
	  		return sfView::SUCCESS ;
	  	}
	  	
	  	$this->challenge->setIsValid(1) ;
	  	$this->challenge->setValidationDt(date('Y-m-d H:i:s')) ;
	  	$this->challenge->save() ;
			
  		/* calculating */
  		$this->calculatePoints($this->challenge) ;
  		
  		return 'Ok' ;
  	}
	}

	public function executeRefuse()	{
		/* loading the challenge */
  	$c = new Criteria() ;
  	$c->add(ChallengePeer::ID, $this->getRequest()->getParameter('challenge')) ;
  	$this->challenge = ChallengePeer::doSelectOneWithPlayers($c) ;
  	
  	if (!$this->challenge)	{
  		$this->forward404() ;
  	}
  	
  	if ($this->getRequest()->getMethod() == sfRequest::POST)	{
	  	if ($this->getUser()->getGuardUser()->getId() != $this->challenge->getP2Id())	{
	  		$this->getRequest()->setError('authorization', 'you are not authorized to refuse this challenge') ;
	  		return sfView::SUCCESS ;
	  	}
	  	
	  	$this->challenge->setReason($this->getRequest()->getParameter('reason')) ;
	  	$this->challenge->setIsRefused(1) ;
	  	$this->challenge->setRefusedAt(date('Y-m-d H:i:s')) ;
	  	$this->challenge->save() ;
  		
  		return 'Ok' ;
  	}
	}

	/* calculating the new points after challenge*/
	public function calculatePoints($challenge)	{
		$p1_points = $challenge->getP1Points() ;
		$p2_points = $challenge->getP2Points() ;
		
		$p1_rating = 1 / (10^(-($p1_points - $p2_points)/400) + 1) ;
		$p2_rating = 1 / (10^(-($p2_points - $p1_points)/400) + 1) ;
		
		$p1_game_points = 1 ;
		$p2_game_points = 0 ;
		
		$p1_new_elo = $p1_points + 32 * ($p1_game_points - $p1_rating) ;
		$p2_new_elo = $p2_points + 32 * ($p2_game_points - $p2_rating) ; 
		
		$challenge->getP1()->setPoints($p1_new_elo) ;
		$challenge->getP2()->setPoints($p2_new_elo) ;

		if ($p1_game_points == 0 )	{
			$this->challenge->getP1()->addLoss() ;
			$this->challenge->getP2()->addWin() ;
		} else if ($p1_game_points == 1) {
			$this->challenge->getP1()->addWin() ;
			$this->challenge->getP2()->addLoss() ;
		} else if ($p1_game_points == 0.5)	{
			$this->challenge->getP1()->addDraw() ;
			$this->challenge->getP2()->addDraw() ;
		}
		
		$this->challenge->getP1()->save() ;
		$this->challenge->getP2()->save() ;
	}
}
